<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数组操作</title>
</head>
<body>

</body>
<script>

    /**
     *   1. 数组的非变异操作
     *       filter find
     *       map   reduce == map-reduce
     *   2.变异操作
     * */

    var arr = [1, 5, 8, 9, 19];
    var employees = [{
        id: 1,
        name: 'Lee',
        gender: 'male'
    }, {
        id: 2,
        name: 'Mat',
        gender: 'female'
    }, {
        id: 3,
        name: 'Tea',
        gender: 'male'
    },]


    var result = arr.filter(function (num) { //定义过滤条件
        return num > 4;
    })

    var arr2 = [];
    for(var index in arr){

         if(arr[index]>4){
              arr2.push(arr[index]);
         }
    }

    var testResult=  arr.reduce(function (prev,curr){
         return prev+curr;
     })

     console.log("testResult:");
     console.log(testResult);

    console.log("arr2:")
    console.log(arr2);



    console.log(result);
    console.log(arr);

    var result2 = arr.find(function (num) {
        return num == 8;
    })


    var result3 = arr.find(function (num) {
        return num == 18;
    })

    var result4 = arr.find(function (num) {
        return num > 8;
    })

    document.writeln(result2);
    document.writeln(result3);
    document.writeln(result4);


    var emp1 = employees.find(function (emp) {
        return emp.name == 'Mat';
    })

    document.writeln(emp1.gender);


   var pingfa=  arr.map(function (num){
         return num*num;
    })

    var newEmployees= employees.map(function (emp){

         return {
             id:emp.id,
             name:'NO'+emp.name,
             gender:emp.gender
         }
    })
    console.log(pingfa);

  var res=  arr.reduce(function (previousValue,currentValue
      ,currentIndex,array){
        return   previousValue+currentValue;
    })


    var res2= arr.map(function (num,index){
         return index%2!=0?num:0
    }).reduce(function (prev,curr){
        return prev+curr;
    })


    var res3= arr.filter(function (num,index){
        return index%2!=0;
    }).reduce(function (prev,curr){
        return prev+curr;
    })

    alert(res3);



</script>
</html>